﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace DunSilverlight.ViewModel
{
    public class ModelBase<TModel,TEntity> : INotifyPropertyChanged , DunLibrary.Core.IItem
        where TModel : DunLibrary.Core.IItem, new()
        where TEntity : DunLibrary.Core.IItem, new()
    {
        protected string name = string.Empty;
        protected int id = 0;
        protected string number = string.Empty;
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        public virtual string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("名称不能为空。");
                }
                this.name = value;
                OnNotifyPropertyChanged("Name");
            }
        }
        public virtual string Number
        {
            get
            {
                return this.number;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("编号不能为空。");
                }
                this.number = value;
                OnNotifyPropertyChanged("Number");
            }
        }

        public Guid GUID { get; set; }

        public string Description { get; set; }

        public bool IsUsed { get; set; }

        public virtual bool Validation()
        {
            if (string.IsNullOrEmpty(Name))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnNotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public virtual TEntity ToEntity()
        {
            TEntity entity = new TEntity();
            entity.Id = this.Id;
            entity.Number = this.Number;
            entity.Name = this.Name;
            entity.GUID = this.GUID;
            entity.IsUsed = this.IsUsed;
            entity.Description = this.Description;
            return entity;
        }

        public static TModel Create(TEntity entity)
        {
            TModel obj = new TModel();
            obj.Id = entity.Id;
            obj.Name = entity.Name;
            obj.Number = entity.Number;
            obj.GUID = entity.GUID;
            obj.IsUsed = entity.IsUsed;
            obj.Description = entity.Description;

            return obj;
        }


       
    }
}
